package com.gexin.service.order;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.order.OrderInvoice;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

public class OrderInvoiceService {

	public static OrderInvoiceService dao = new OrderInvoiceService();

	public Page<OrderInvoice> findList(Integer page , Integer pageSize , String id , Integer state,String times) {
		return  OrderInvoice.dao.findList(page,pageSize,id,state,times);
	}

	/**
	 * 保存订单发票
	 * @param orderId 订单id
	 * @param type 发票类型
	 * @param header 发票抬头
	 * @param ein 税号
	 * @param email 收件邮箱
	 * @param userName 用户名称
	 * @param companyName 公司名称
	 * @param bankName 开户行名称
	 * @param bankNumber 银行卡卡号
	 * @return
	 */
	public ApiResponse saveInvoice(String orderId, Integer type, String header, String ein, String email,String userName,String companyName,String bankName,String bankNumber){
		if(orderId==null)
			return new ApiResponse(false,"订单id不能为null!");
		if(type==null)
			return new ApiResponse(false,"发票类型不能为null");
		if(StringUtil.isBlank(header))
			return new ApiResponse(false,"发票抬头不能为空!");

		if(StringUtil.isBlank(email))
			return new ApiResponse(false,"收件邮箱不能为空!");

		if(OrderInvoice.TYPE_USER.equals(type)){
			if(StringUtil.isBlank(userName))
				return new ApiResponse(false,"用户名称不能为空");
		}else{
			if(StringUtil.isBlank(companyName))
				return new ApiResponse(false,"公司名称不能为空");
			if(StringUtil.isBlank(ein))
				return new ApiResponse(false,"税号不能为空!");
		}

		OrderInvoice orderInvoice = OrderInvoice.dao.findById(orderId);
		if (orderInvoice!=null){
			return new ApiResponse(false,"该订单已经关联了发票信息");
		}
		orderInvoice = new OrderInvoice();
		orderInvoice.setEin(ein);
		orderInvoice.setType(type);
		orderInvoice.setHeader(header);
		orderInvoice.setId(orderId);
		orderInvoice.setEmail(email);
		orderInvoice.setState(OrderInvoice.STATE_SUBMIT);
		orderInvoice.setBankName(bankName);
		orderInvoice.setBankNumber(bankNumber);
		orderInvoice.setCompanyName(companyName);
		orderInvoice.setUserName(userName);
		orderInvoice.setCreateTime(new Date());
		boolean save = orderInvoice.save();
		return new ApiResponse(save,save?"保存成功!":"保存失败!");
	}


}
